from rest_framework.utils import field_mapping
from webevent.models import WebEnvents
from rest_framework import serializers
import webevent.events as events

class WebEventSerializer(serializers.ModelSerializer):
  username = serializers.CharField(source='user.real_name')
  detail = serializers.SerializerMethodField()
  create_ts = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")

  class Meta:
    model=WebEnvents
    fields=['id', 'username', 'detail', 'create_ts']

  def get_detail(self, obj):
    return events.get_event_detail(obj.event, obj.data)

class WebEventCreateSerializer(serializers.ModelSerializer):
  class Meta:
    model = WebEnvents
    fields = ['event', 'data', 'ext_pk']
  def create(self, validated_data):
    user = self.context['request'].user
    validated_data['user'] = user
    return super().create(validated_data)
